﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DTO;
using BUS;
using TuyenDungViecLam.CommonResource;

namespace TuyenDungViecLam.User_control_VN
{
    public partial class Detail_Major_In_Search : System.Web.UI.UserControl
    {

        protected void dscongviec_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            dscongviec.PageIndex = e.NewPageIndex;
            LienKetDuLieu();
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (Session["Role"].ToString() == "NhaTuyenDung" || Session["Role"].ToString() == "Administrator")
            {
                Button3.Visible = false;
                Button4.Visible = false;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {

            LienKetDuLieu();
        }

        public void LienKetDuLieu()
        {
            int Search;
            Search = int.Parse(Request.QueryString["Search"].ToString());

            if (Search == 0)
            {
                try
                {
                    int Co = 0;
                    int Snd = -1;
                    int Mcb = 0;
                    int Snkn = -1;
                    int Mlhcv = 0;
                    int Lmax = -1;
                    int Lmin = -1;
                    int Mnlv = -1;
                    int Mng = -1;

                    if (Request.QueryString["Mng"] != null)
                    {
                        Mng = int.Parse(Request.QueryString["Mng"].ToString());
                        Co++;
                    }
                    if (Request.QueryString["Mnlv"] != null)
                    {
                        Mnlv = int.Parse(Request.QueryString["Mnlv"].ToString());
                        Co++;
                    }
                    if (Request.QueryString["Snd"] != null)
                    {
                        Snd = int.Parse(Request.QueryString["Snd"]);
                        Co++;
                    }
                    if (Request.QueryString["Mcb"] != null)
                    {
                        Mcb = int.Parse(Request.QueryString["Mcb"]);
                        Co++;
                    }
                    if (Request.QueryString["Snkn"] != null)
                    {
                        Snkn = int.Parse(Request.QueryString["Snkn"]);
                        Co++;
                    }
                    if (Request.QueryString["Mlhcv"] != null)
                    {
                        Mlhcv = int.Parse(Request.QueryString["Mlhcv"]);
                        Co++;
                    }

                    if (Request.QueryString["Lmin"] != null)
                    {
                        if (Request.QueryString["Lmax"] != "" && Request.QueryString["Lmin"] == "")
                        {
                            Lmax = int.Parse(Request.QueryString["Lmax"]);
                            Co++;
                        }

                        if (Request.QueryString["Lmax"] == "" && Request.QueryString["Lmin"] != "")
                        {
                            Lmin = int.Parse(Request.QueryString["Lmin"]);
                            Co++;
                        }

                        if (Request.QueryString["Lmax"] != "" && Request.QueryString["Lmin"] != "")
                        {
                            Lmin = int.Parse(Request.QueryString["Lmin"]);
                            Lmax = int.Parse(Request.QueryString["Lmax"]);
                            Co++;
                        }
                    }
                    dscongviec.DataSource = ChiTietCongViec_BUS.TimKiemNhanhDanhSachCacCongViecThuocNhaTuyenDung(Request.QueryString["Mct"], Mng, Mnlv, Snd, Mcb, Snkn, Mlhcv, Lmin, Lmax, Co);
                    dscongviec.DataBind();

                }
                catch (Exception)
                {
                    Response.Redirect(Request.Url.AbsoluteUri);
                }
            }

            if (Search == 1)
            {
                try
                {
                    int Co = 3;
                    int Snd = -1;
                    int Mcb = 0;
                    int Snkn = -1;
                    int Mlhcv = 0;
                    int Lmax = -1;
                    int Lmin = -1;
                    string TuKhoa = Request.QueryString["TuKhoa"].ToString();
                    int Mnlv = int.Parse(Request.QueryString["Mnlv"].ToString());
                    int Mng = int.Parse(Request.QueryString["Mng"].ToString());
                    if (Request.QueryString["Snd"] != null)
                    {
                        Snd = int.Parse(Request.QueryString["Snd"]);
                        Co++;
                    }
                    if (Request.QueryString["Mcb"] != null)
                    {
                        Mcb = int.Parse(Request.QueryString["Mcb"]);
                        Co++;
                    }
                    if (Request.QueryString["Snkn"] != null)
                    {
                        Snkn = int.Parse(Request.QueryString["Snkn"]);
                        Co++;
                    }
                    if (Request.QueryString["Mlhcv"] != null)
                    {
                        Mlhcv = int.Parse(Request.QueryString["Mlhcv"]);
                        Co++;
                    }

                    if (Request.QueryString["Lmin"] != null)
                    {
                        if (Request.QueryString["Lmax"] != "" && Request.QueryString["Lmin"] == "")
                        {
                            Lmax = int.Parse(Request.QueryString["Lmax"]);
                            Co++;
                        }

                        if (Request.QueryString["Lmax"] == "" && Request.QueryString["Lmin"] != "")
                        {
                            Lmin = int.Parse(Request.QueryString["Lmin"]);
                            Co++;
                        }

                        if (Request.QueryString["Lmax"] != "" && Request.QueryString["Lmin"] != "")
                        {
                            Lmin = int.Parse(Request.QueryString["Lmin"]);
                            Lmax = int.Parse(Request.QueryString["Lmax"]);
                            Co++;
                        }
                    }
                    dscongviec.DataSource = ChiTietCongViec_BUS.TimKiemNhanh(TuKhoa, Mng, Mnlv, Snd, Mcb, Snkn, Mlhcv, Lmin, Lmax, Co);
                    dscongviec.DataBind();
                }
                catch (Exception)
                {
                    Response.Redirect(Request.Url.AbsoluteUri);
                }
            }

            if (Search == 2)
            {
                try
                {
                    if (Request.QueryString["Mng[]"] != "" || Request.QueryString["Mng"] != null)
                    {
                        int Co2 = 7;
                        if (Request.QueryString["Snkn"] != null)
                            Co2 = 8;
                        int Mnlv = int.Parse(Request.QueryString["Mnlv"]);
                        int Snd = int.Parse(Request.QueryString["Snd"]);
                        int Mcb = int.Parse(Request.QueryString["Mcb"]);
                        int Mlhcv = int.Parse(Request.QueryString["Mlhcv"]);
                        int Lmin = -1;
                        if (Request.QueryString["Lmin"] != "")
                        {
                            Lmin = int.Parse(Request.QueryString["Lmin"]);
                        }
                        int Lmax = -1;
                        if (Request.QueryString["Lmax"] != "")
                        {
                            Lmax = int.Parse(Request.QueryString["Lmax"]);
                        }
                        dscongviec.DataSource = ChiTietCongViec_BUS.TimKiemNangCao(Request.QueryString["TuKhoa"], int.Parse(Request.QueryString["LoaiTuKhoa"]), Request.QueryString["Mng[]"], Request.QueryString["Mng"], Mnlv, Snd, Mcb, Request.QueryString["Snkn"], Mlhcv, Lmin, Lmax, Co2);
                        dscongviec.DataBind();

                    }

                }
                catch (Exception)
                {
                    Response.Redirect(Request.Url.AbsoluteUri);
                }
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            if (Session["Role"].ToString() == "NguoiTimViec")
            {
                for (int i = 0; i < dscongviec.Rows.Count; i++)
                {
                    Label lbmacongviec = (Label)dscongviec.Rows[i].Cells[0].FindControl("lbmacongviec");
                    CheckBox cbchon = (CheckBox)dscongviec.Rows[i].Cells[0].FindControl("cbchon");
                    if (cbchon.Checked == true)
                    {
                        NguoiTimViec_BUS.LuuCongViecDaXem(int.Parse(Session["UserID"].ToString()), int.Parse(lbmacongviec.Text), DateTime.Now);
                    }
                }
                string url = "../NguoiTimViec/DanhSachCongViecDaLuu.aspx";
                string Message = "Đã lưu công việc này cho bạn !";
                Alert.ShowAndRedirect(Message, url);
            }
            else
            {
                string Mcv = "";
                for (int i = 0; i < dscongviec.Rows.Count; i++)
                {
                    Label lbmacongviec = (Label)dscongviec.Rows[i].Cells[0].FindControl("lbmacongviec");
                    CheckBox cbchon = (CheckBox)dscongviec.Rows[i].Cells[0].FindControl("cbchon");
                    if (cbchon.Checked == true)
                    {
                        Mcv += lbmacongviec.Text + ",";
                    }
                }
                Mcv = Mcv.Remove(Mcv.Length - 1);
                string url = "DangNhap.aspx?Mcv=" + Mcv;
                string Message = "Vui lòng đăng nhập để lưu lại công việc này cho bạn !";
                Alert.ShowAndRedirect(Message, url);
            }
        }

    }
}